/*
 * WidgetClickeable.cpp
 *
 *  Created on: Apr 21, 2012
 *      Author: gonzalo
 */

#include "WidgetClickeable.h"

void WidgetClickeable::init(int posX, int posY)
{
    //Set the surface of the widget, the unpressed one.
    this->surface = unpressedSurface;
    this->pressed =false;

    initParcel(posX, posY);
}

WidgetClickeable::WidgetClickeable(Color color,int width, int height,int posX, int posY, Widget* parent)
:Widget(posX,posY,NULL,parent, true){


	Color* opuesto = color.opuesto();
	this->pressedSurface = SurfaceUtils::colorSurface(width,height,*(opuesto));
	this->unpressedSurface = SurfaceUtils::colorSurface(width,height,color);

	delete opuesto;
    init(posX,posY);
}

WidgetClickeable::WidgetClickeable(std::string path,int posX, int posY,  Widget* parent)
:Widget(posX,posY,NULL,parent, true){

	std::string pressedSurfacePath = IMAGES_DEFAULT_FOLDER + path + IMAGES_DEFAULT_PRESSED;
	std::string unpressedSurfacePath = IMAGES_DEFAULT_FOLDER + path + IMAGES_DEFAULT_UNPRESSED;

	this->pressedSurface = SurfaceUtils::loadImage(pressedSurfacePath);
	this->unpressedSurface = SurfaceUtils::loadImage(unpressedSurfacePath);

	init(posX,posY);
}

void WidgetClickeable::resolve() {
	if (this->pressed) {
		this->surface = this->pressedSurface;
	} else if (!this->pressed) {
		this->surface= this->unpressedSurface;
	}

	Widget::resolve();
}

WidgetClickeable::~WidgetClickeable(){
	this->surface = NULL;
	//The surfaces belong to the widgetClickeable
	SDL_FreeSurface(this->pressedSurface);
	SDL_FreeSurface(this->unpressedSurface);
}
